//
//  HMNetWorkTools.swift
//  swift网络封装
//
//  Created by 程双岐 on 16/9/21.
//  Copyright © 2016年 heima. All rights reserved.
//

import UIKit
import AFNetworking

enum Requset: Int {
    case GET
    case POST
}

class HMNetWorkTools: AFHTTPSessionManager {
    

    
    static let sharedTool : HMNetWorkTools = {
        let tool = HMNetWorkTools()
        tool.responseSerializer.acceptableContentTypes?.insert("text/html")
        tool.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tool
    }()
    
    func requset(type: Requset,url: String,params: Any?, callBack: @escaping (Any?,Error?)->()) -> Void {
        if type == .GET {
            get(url, parameters: params, progress: nil, success: { (_, response) in
                callBack(response, nil)
                }, failure: { (_, erreo) in
                    callBack(nil,erreo)
            })
        }else{
            
            post(url, parameters: params, progress: nil, success: { (_, response) in
                callBack(response,nil)
                }, failure: { (_, erreo) in
                    callBack(nil,erreo)
            })
            
        }
    }
    

    

}
extension HMNetWorkTools{
    
    func upload(access_token: String, status: String, image: UIImage, callBack: @escaping (Any?, Error?)->()) -> Void {
        //  准备url地址
        let url = "https://upload.api.weibo.com/2/statuses/upload.json"
        //  准备参数
        let params = [
            "access_token": access_token,
            "status": status
        ]
        
        //let imageData = UIImagePNGRepresentation(image)!
        //  compressionQuality 质量系数0-1, 质量系数越大表示图片越清晰
        let imageData = UIImageJPEGRepresentation(image, 0.5)!
        
        
        
        
        //  上传请求接口
        post(url, parameters: params, constructingBodyWith: { (formData) in
            
            //  withFileData 图片的二进制数据
            //  name 表示服务端获取图片的所需要的参数
            //  fileName 表示图片的文件名字 -> 可以随便写,因为服务的会给我们生成一个唯一的图片名
            //  mimeType 图片的资源
            formData.appendPart(withFileData: imageData, name: "pic", fileName: "test", mimeType: "image/jpeg")
            }, progress: nil, success: { (_, response) in
                callBack(response, nil)
        }) { (_, error) in
            callBack(nil, error)
        }
        
    }
    
    func requserStatuses(accessToken: String,sinceId: Int64, maxId: Int64, callBack:@escaping (Any?, Error?) -> ()){
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        
        let params: [String: Any] = [
            "access_token" : accessToken,
            "max_id" : maxId,
            "since_id" : sinceId
        ]
        print(url + "?access_token=\(accessToken)")
        
        requset(type: .GET, url: url, params: params, callBack: callBack)
        
    }
}


extension HMNetWorkTools{
    func update(access_token: String,status: String, callBack:@escaping (Any?, Error?) -> ()) -> Void {
        let url = "https://api.weibo.com/2/statuses/update.json"
        
        let params = [
            "access_token": access_token,
            "status": status
        ]
        requset(type: .POST, url: url, params: params, callBack: callBack)
        
        
    }
}

extension HMNetWorkTools {
    
    func requsetUsrtInfo(accessToken: String,uid: Int64,callBack: @escaping (Any?,Error?)->()){
        let url = "https://api.weibo.com/2/users/show.json"
        
        //MARK:多了一个问号 
        let params:[String : Any] = [
            "access_token" : accessToken,
            "uid" : uid
        ]
        
        self.requset(type: .GET, url: url, params: params, callBack: callBack)
    }
    
    
    
    func requsetAccessToKen(code : String ,callBack:@escaping (Any?,Error?)->()){
        let url = "https://api.weibo.com/oauth2/access_token"
        let params = [
            "client_id" : key,
            "client_secret" : WeiboAppSecret,
            "grant_type" : "authorization_code",
            "code" : code,
            "redirect_uri" : https
        ]
        
        self .requset(type: .POST, url: url, params: params, callBack: callBack)
        
    }
}
